import { defineStore } from "pinia";
import { ref } from "vue";
import { loginAPI } from '@/apis/user';
import { useCartStore } from "./cartStore";
export const useUserStore = defineStore('user', () => {
    const userInfo = ref({})
    const cartStore = useCartStore()
    //用户登录
    const getUserInfo = async ({ username, password }) => {
        const result = await loginAPI({ username, password })
        userInfo.value = result.data
        //获取购物车数据
        cartStore.refreshCart()
    }
    //退出登录
    const removeUserInfo = () => {
        userInfo.value = {}
        //清除购物车数据
        cartStore.clearCart()
    }
    return {
        userInfo,
        removeUserInfo,
        getUserInfo
    }
},
    {
        persist: true,
    }
)
